package textdiff {
	import mx.collections.ArrayCollection;
	
	
	/**
	 * Append a block of lines to the end of the old file.
	 */
	public class AppendCommand extends EditCommand {
	
	    public function AppendCommand(oldFileInfo:FileInfo=null, newFileInfo:FileInfo=null) {
	        super( oldFileInfo, newFileInfo );
	        command = "Append";
	        if (newFileInfo != null) {
	        	newLines = newFileInfo.nextBlock();
	        	newLines.reportable = true;
	        }
	    }
	    
	    public override function toEditScript(): String {
	    	return newLines.fromLineNum + "a\r" + newLines.textLines + ".\r";
	    }
	    
	    public override function editText(t:Array, offset:int=0): Array {
	    	// make a copy of the input array t (using Array.slice) and wrap it in an ArrayCollection
	    	var lines:ArrayCollection = new ArrayCollection(t.slice());
	    	
	    	// if needed, increase the length of the result array to a size that fits the result, 
	    	// taking the start offset into account
	    	//while (lines.length < oldLines.fromLineNum+newLines.lines.length) lines.addItem(""); 

	    	for (var i:int=0; i<newLines.lines.length; i++) {
    			// we have prepared for this situation, because we grew a large enough array,
    			// so there is an empty slot at the insert position that we can safely replace 
	    		lines.addItem(newLines.lines[i]);
	    	}
	    	return lines.toArray();
	    }
	    
	    public override function get fromLineNum(): int { return newLines.fromLineNum; }
	    public override function get thruLineNum(): int { return newLines.thruLineNum; }
	}
}
